package io.wongxd.demo.page.mask

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.wongxd.solution.compose.common.debouncedClickable
import io.wongxd.solution.compose.composeTheme.AppTheme
import io.wongxd.solution.util.statusBar
import io.wongxd.solution.weight.maskSwitch.MaskSwitchAnimModel
import io.wongxd.solution.weight.maskSwitch.MaskSwitchBox
import io.wongxd.solution.weight.maskSwitch.activeMaskView


class MaskActivity : ComponentActivity() {
    @SuppressLint("SuspiciousIndentation")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                Render()
//                XmlScreen()
            }
        }
    }

    private val doSwitchMs = mutableStateOf(0L)
    private val maskClickX = mutableStateOf(0f)
    private val maskClickY = mutableStateOf(0f)

    @Composable
    fun Render() {
        val ctx = LocalContext.current
        val scope = rememberCoroutineScope()
        val lifecycleOwner = LocalLifecycleOwner.current

        var buttonActive by remember { mutableStateOf(true) }
        var isDarkTheme by remember { mutableStateOf(false) }

        LaunchedEffect(key1 = isDarkTheme, block = {
            if (isDarkTheme) statusBar.color(android.graphics.Color.GRAY).apply() else statusBar.color(android.graphics.Color.GREEN).apply()
        })

        MaskSwitchBox(
            maskComplete = {
                buttonActive = false
            }, animFinish = {
                buttonActive = true
            }) { maskActiveEvent ->

            LaunchedEffect(doSwitchMs.value) {
                if (doSwitchMs.value <= 0L) return@LaunchedEffect
                if (!buttonActive) return@LaunchedEffect

                if (isDarkTheme)
                    maskActiveEvent(MaskSwitchAnimModel.SHRINK, maskClickX.value, maskClickY.value)
                else
                    maskActiveEvent(MaskSwitchAnimModel.EXPEND, maskClickX.value, maskClickY.value)
                isDarkTheme = !isDarkTheme
            }

            ComposeScreen(isDarkTheme = isDarkTheme)

        }
    }

    @Composable
    fun ComposeScreen(isDarkTheme: Boolean) {

        Box(Modifier.background(if (isDarkTheme) Color.Gray else Color.Green)) {
            LazyColumn {
                repeat(1000) {
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(60.dp),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(text = "$it")
                        }
                    }
                }
            }

            Box(modifier = Modifier
                .padding(35.dp)
                .height(50.dp)
                .width(100.dp)
                .debouncedClickable {
                    doSwitchMs.value = System.currentTimeMillis()
                }) {

                Text(text = if (isDarkTheme) "toLight" else "toDark", modifier = Modifier.onGloballyPositioned { coordinates ->
                    maskClickX.value = coordinates.boundsInRoot().center.x
                    maskClickY.value = coordinates.boundsInRoot().center.y
                })

            }
        }
    }

    @Composable
    fun XmlScreen() {
        val context = LocalContext.current
        val scope = rememberCoroutineScope()

        var clickX by remember { mutableStateOf(0f) }
        var clickY by remember { mutableStateOf(0f) }

        var buttonActive by remember { mutableStateOf(true) }
        var isDarkTheme by remember { mutableStateOf(false) }

        LaunchedEffect(key1 = isDarkTheme, block = {
            if (isDarkTheme) statusBar.color(android.graphics.Color.GRAY).apply() else statusBar.color(android.graphics.Color.GREEN).apply()
        })

        Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .background(if (isDarkTheme) Color.Gray else Color.Green)
                    .padding(10.dp)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 10.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(text = "customView")
                    Button(
                        enabled = buttonActive,
                        onClick = {
                            buttonActive = false
                            context.activeMaskView(
                                animModel = if (isDarkTheme) MaskSwitchAnimModel.SHRINK else MaskSwitchAnimModel.EXPEND,
                                clickX = clickX,
                                clickY = clickY,
                                animTime = 700,
                                maskComplete = {
                                    isDarkTheme = !isDarkTheme
                                },
                                maskAnimFinish = {
                                    buttonActive = true
                                },
                            )
                        }
                    ) {
                        val tipContent = if (isDarkTheme) "ToLightTheme" else "ToDarkTheme"
                        Text(
                            text = tipContent,
                            modifier = Modifier
                                .onGloballyPositioned { coordinates ->
                                    clickX = coordinates.boundsInWindow().center.x
                                    clickY = coordinates.boundsInWindow().center.y
                                },
                        )
                    }
                }
                LazyColumn {
                    items(50) {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 10.dp)
                        ) {
                            Row(
                                Modifier
                                    .fillMaxWidth()
                                    .padding(15.dp),
                                verticalAlignment = Alignment.CenterVertically,
                                horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(
                                    text = it.toString(),
                                    fontSize = 30.sp,
                                    fontWeight = FontWeight.Bold
                                )
                            }
                        }
                    }
                }
            }
        }
    }


}

